import Taro, { Component } from '@tarojs/taro'
import { View, Text, Image, Picker} from '@tarojs/components'
import { AtList, AtListItem, AtTextarea,AtForm, AtInput, AtAvatar,AtButton} from "taro-ui"
import './zizhibeiantupian.css'
import { $redirectTo, getGlobalData } from '../../../../global';
import app from '../../../../main.js'
// import identityCard1  from '../../../../img/identityCard1.png'
// import identityCard2 from '../../../../img/identityCard2.png'
// import doctorQualification1 from '../../../../img/doctorQualification1.jpg'
// import doctorQualification2 from '../../../../img/doctorQualification2.jpg'
// import practiceCard1 from '../../../../img/practiceCard1.jpg'
// import practiceCard2 from '../../../../img/practiceCard2.jpg'
// import titleCard1 from '../../../../img/titleCard1.jpg'
// import titleCard2 from '../../../../img/titleCard2.jpg'
// import facePhoto from '../../../../img/facePhoto.jpg'


export default class Zizhibeiantupian extends Component {

    config = {  
        navigationBarTitleText: '资质备案',
    }

    constructor(props) {
        super(...arguments,props);
        this.state = {
            sfzL:'',         //身份证
            sfzR:'',
            sfz:1,
            zgzL:'',         //医师资格证
            zgzR:'',
            zgz:1,
            zyzL:'',         //医师执业证
            zyzR:'',
            zyz:1,
            zczL:'',         //医师职称证
            zczR:'',
            zcz:1,
            zmz:''           //本人正面照
        }
    }
   
    //拆分字符串
    queryURLParamteter(url){
        var pic = {aPic:'',bPic:''};
        try {
            if (app.isEmpty(url)) {
                return pic;
            }
            var arr = JSON.parse(url);
            pic.aPic = arr.length >= 1 ? arr[0] : '';
            pic.bPic = arr.length >= 2 ? arr[1] : '';
        } catch (error) {
            console.log('error', error);
        }
        return pic;
    }

    getDoctor () {
        let me = this
        let data = {}
        app.get('web/doctor/get', data, (res)=>{
            me.setState({
                sfzL:me.queryURLParamteter(res.data.result.idcard_pics).aPic || '',
                sfzR:me.queryURLParamteter(res.data.result.idcard_pics).bPic || '',
                zgzL:me.queryURLParamteter(res.data.result.qualification_pics).aPic || '',
                zgzR:me.queryURLParamteter(res.data.result.qualification_pics).bPic || '',
                zyzL:me.queryURLParamteter(res.data.result.practicing_pics).aPic || '',
                zyzR:me.queryURLParamteter(res.data.result.practicing_pics).bPic || '',
                zczL:me.queryURLParamteter(res.data.result.profession_pics).aPic || '',
                zczR:me.queryURLParamteter(res.data.result.profession_pics).aPic || '',
                zmz:res.data.result.head_image
            })
        })
    }

    uploadFile(id) {
        var me = this;
        Taro.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths
                Taro.uploadFile({
                    url: app.apiUri + '/admin/upload/image',
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        name: 'file'
                    },
                    success(res) {
                        let data = JSON.parse(res.data);
                        if (data.status == 200) {
                            let img = data.result;
                            if(id == 1){
                                if(me.state.sfz == 1){
                                    me.setState({
                                        sfzL:img,
                                        sfz:2
                                    })
                                }else{
                                    me.setState({
                                        sfzR:img,
                                        sfz:1
                                    })
                                }
                            }
                            if(id == 2){
                                if(me.state.zgz == 1){
                                    me.setState({
                                        zgzL:img,
                                        zgz:2
                                    })
                                }else{
                                    me.setState({
                                        zgzR:img,
                                        zgz:1
                                    })
                                }
                            }
                            if(id == 3){
                                if(me.state.zyz == 1){
                                    me.setState({
                                        zyzL:img,
                                        zyz:2
                                    })
                                }else{
                                    me.setState({
                                        zyzR:img,
                                        zyz:1
                                    })
                                }
                            }
                            if(id == 4){
                                if(me.state.zcz == 1){
                                    me.setState({
                                        zczL:img,
                                        zcz:2
                                    })
                                }else{
                                    me.setState({
                                        zczR:img,
                                        zcz:1
                                    })
                                }
                            }
                            if(id == 5){
                                me.setState({
                                    zmz:img,
                                })                              
                            }
                        }else{
                            Taro.showModal({
                                title: '提示',
                                content: '上传失败，请稍后再试',
                                showCancel:false
                            })
                        }
                    }
                })
            }
        })
    }

    go(){
        let data = {
            idcard_pics: JSON.stringify([this.state.sfzL,this.state.sfzR]),
            qualification_pics: JSON.stringify([this.state.zgzL,this.state.zgzR]), 
            practicing_pics: JSON.stringify([this.state.zyzL,this.state.zyzR]), 
            profession_pics: JSON.stringify([this.state.zczL,this.state.zczR]),
            head_image: this.state.zmz
        }
        if (app.isEmpty(this.state.sfzL) && app.isEmpty(this.state.sfzR)) {
            app.toastMessage('请上传身份证');
            return;
        } else {
            var idcard_pics = [];
            if (!app.isEmpty(this.state.sfzL)) {
                idcard_pics.push(this.state.sfzL);
            }
            if (!app.isEmpty(this.state.sfzR)) {
                idcard_pics.push(this.state.sfzR);
            }
            data.idcard_pics = JSON.stringify(idcard_pics);
        }

        if (app.isEmpty(this.state.zgzL) && app.isEmpty(this.state.zgzR)) {
            app.toastMessage('请上传医师资格证');
            return;
        } else {
            var qualification_pics = [];
            if (!app.isEmpty(this.state.zgzL)) {
                qualification_pics.push(this.state.zgzL);
            }
            if (!app.isEmpty(this.state.zgzR)) {
                qualification_pics.push(this.state.zgzR);
            }
            data.qualification_pics = JSON.stringify(qualification_pics);
        }

        if (app.isEmpty(this.state.zyzL) && app.isEmpty(this.state.zyzR)) {
            app.toastMessage('请上传医师职业证');
            return;
        } else {
            var practicing_pics = [];
            if (!app.isEmpty(this.state.zyzL)) {
                practicing_pics.push(this.state.zyzL);
            }
            if (!app.isEmpty(this.state.zyzR)) {
                practicing_pics.push(this.state.zyzR);
            }
            data.practicing_pics = JSON.stringify(practicing_pics);
        }

        if (app.isEmpty(this.state.zczL) && app.isEmpty(this.state.zczR)) {
            app.toastMessage('请上传医师职称证');
            return;
        } else {
            var profession_pics = [];
            if (!app.isEmpty(this.state.zczL)) {
                profession_pics.push(this.state.zczL);
            }
            if (!app.isEmpty(this.state.zczR)) {
                profession_pics.push(this.state.zczR);
            }
            data.profession_pics = JSON.stringify(profession_pics);
        }

        if (app.isEmpty(this.state.zmz)) {
            app.toastMessage('请上传本人正面照');
            return;
        } else {
            data.head_image = this.state.zmz;
        }

        app.post('web/doctor/save_pics', data, function(res){
            if(res.data.status == 200){
                app.redirectTo('../../../doctorpages/homepage/homepage')
            }else{
                Taro.showModal({
                    title: '提示',
                    content: '提交失败',
                    showCancel:false
                })
            }        
        })    
    }

    showImage(image) {
        console.log(image)
        app.prviewImage(image, [image]);
    }

    componentDidMount () {
        this.getDoctor()
    }

    render() {
        let sfz1 = this.state.sfzL == '' ? (app.imageUri+'img/identityCard1.png') : this.state.sfzL
        let sfz2 = this.state.sfzR == '' ? (app.imageUri+'img/identityCard2.png') : this.state.sfzR
        let zgz1 = this.state.zgzL == ''? (app.imageUri+'img/doctorQualification1.jpg') : this.state.zgzL
        let zgz2 = this.state.zgzR == ''? (app.imageUri+'img/doctorQualification2.jpg') : this.state.zgzR
        let zyz1 = this.state.zyzL == '' ? (app.imageUri+'img/practiceCard1.jpg') : this.state.zyzL
        let zyz2 = this.state.zyzR == '' ? (app.imageUri+'img/practiceCard2.jpg') : this.state.zyzR
        let zcz1 = this.state.zczL == '' ? (app.imageUri+'img/titleCard1.jpg') : this.state.zczL
        let zcz2 = this.state.zczR == '' ? (app.imageUri+'img/titleCard2.jpg') : this.state.zczR
        let zmz1 = this.state.zmz == '' ? (app.imageUri+'img/facePhoto.jpg') : this.state.zmz
        console.log(sfz1)
        return (
            <View className='zizhibeiantupian'>
                <View className='head'>
                    <View className='head-text'>请提供清晰,完整的证件原件照片，上传的所有资料仅用于资质备案对所有第三方都保密不可见</View>
                </View>

                <View className='list'>
                    <View className='list-text'>身份证(请上传身份证的正反面照片)</View>
                </View>
                <View className='image'>
                    <View className='upload' onClick={this.uploadFile.bind(this,1)}>+</View>
                    <View className='image-box'>
                        <Image className='img' src={sfz1} onClick={this.showImage.bind(this, sfz1)} />
                        <Image className='img' src={sfz2} onClick={this.showImage.bind(this, sfz2)} />
                    </View>
                </View>

                <View className='list'>
                    <View className='list-text'>医师资格证(请上传全部非空白页)</View>
                </View>
                <View className='image'>
                    <View className='upload' onClick={this.uploadFile.bind(this,2)}>+</View>
                    <View className='image-box'>
                        <Image className='img' src={zgz1} onClick={this.showImage.bind(this, zgz1)} />
                        <Image className='img' src={zgz2} onClick={this.showImage.bind(this, zgz2)} />
                    </View>
                </View>

                <View className='list'>
                    <View className='list-text'>医师执业证(请上传全部非空白页)</View>
                </View>
                <View className='image'>
                    <View className='upload' onClick={this.uploadFile.bind(this,3)}>+</View>
                    <View className='image-box'>
                        <Image className='img' src={zyz1} onClick={this.showImage.bind(this, zyz1)} />
                        <Image className='img' src={zyz2} onClick={this.showImage.bind(this, zyz2)} />
                    </View>
                </View>

                <View className='list'>
                    <View className='list-text'>医师职称证(请上传全部非空白页)</View>
                </View>
                <View className='image'>
                    <View className='upload' onClick={this.uploadFile.bind(this,4)}>+</View>
                    <View className='image-box'>
                        <Image className='img' src={zcz1} onClick={this.showImage.bind(this, zcz1)} />
                        <Image className='img' src={zcz2} onClick={this.showImage.bind(this, zcz2)} />
                    </View>
                </View>

                <View className='list'>
                    <View className='list-text'>本人正面照(请上传近期本人的正面上身照片)</View>
                </View>
                <View className='image'>
                    <View className='upload' onClick={this.uploadFile.bind(this,5)}>+</View>
                    <View className='image-box'>
                        <Image className='img' src={zmz1} onClick={this.showImage.bind(this, zmz1)} />
                    </View>
                </View>
                <View className='shuoming'>提示：本正面照会在医生主页上展示给患者,请尽量身穿工作服,在纯色背景下拍摄清晰照片上传</View>

                <View style="height:50px;width:100%;"></View>
                <AtButton className='btn' type='primary' onClick={this.go.bind(this)}>提交</AtButton>

            </View>
        )
    }
}
